﻿@typeparam TItem

<table class="table table-hover">
    @if (HeaderTemplete is not null)
    {
        <thead>
            <tr>
                @HeaderTemplete
            </tr>
        </thead>
    }
    <tbody>
        @if (Data is null)
        {
            <tr>
                <td colspan="3">Loading...</td>
            </tr>
        }
        else
        {
            @foreach (var item in Data)
            {
                <tr>
                    @RowTemplate(item)
                </tr>
            }
        }
    </tbody>
    @if (Data is not null)
    {

        <tfoot>
            <tr>
                @FooterTemplate?.Invoke(Data?.Count())
            </tr>
        </tfoot>
    }
</table>


@code {
    IEnumerable<TItem>? Data { get; set; }

    [Parameter] public RenderFragment? HeaderTemplete { get; set; }
    [Parameter] public RenderFragment<TItem>? RowTemplate { get; set; }
    [Parameter] public RenderFragment<int?>? FooterTemplate { get; set; }
    [Parameter] public Func<IEnumerable<TItem>>? OnDataQuery { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            System.Threading.Thread.Sleep(3000);            
            Data = OnDataQuery?.Invoke();
            StateHasChanged();
        }
    }
}